<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends MY_Controller{
    function __construct()
    {
        parent::__construct();
    }

    public function index() {
       $this->load->helper('url');//辅助函数
        $this->load->model('TestModel', 'test');



        $this->load->library('pagination');
        $config['base_url'] = 'http://127.0.0.1/ci_test/CodeIgniter-3.1.6/index.php/Home/home/index';
        $cur_page = $this->input->get('per_page');
        if (!$cur_page) {
            $cur_page = 1;
        }
        $config['total_rows'] = 20;
        $config['per_page'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['page_query_string'] = TRUE;
        $this->pagination->initialize($config);
        echo $this->pagination->create_links();

        //echo uri_string();
        echo $this->router->fetch_class();
        echo $this->router->fetch_method(); 
        $list = $this->test->getList(array(), $cur_page);
        $coun = $this->test->getCount();echoarr($coun);
        $data = array();
        $data['list'] = $list;
        $this->load->view('home/index', $data);
    }
    public function add() {
        $this->load->helper('url');//辅助函数
        if ($this->input->post()) {
            $data = array();
            $data['name'] = $this->input->post('name');
            $data['click_times'] = $this->input->post('clicktimes');

            $this->load->model('TestModel', 'test');
            $this->test->add($data);
            echo '<script>alert("添加成功");window.location.href="'.site_url('Home/home/index').'"</script>';
        }
        $this->load->view('home/add');
    }

    public function update()  {
        $this->load->helper('url');//辅助函数
        $id = $this->input->get('id');
        $this->load->model('TestModel', 'test');
        $info = $this->test->getone(array('id'=>$id));
        if ($this->input->post()) {
            $edit_data = array();
            $edit_data['name'] = $this->input->post('name');
            $edit_data['click_times'] = $this->input->post('clicktimes');
            $this->test->update(array('id'=>$id), $edit_data);
            echo '<script>alert("修改成功");window.location.href="'.site_url('Home/home/index').'"</script>';
        }
        $data = array();
        $data['info'] = $info;
        $this->load->view('home/update', $data);
    }
    public function del() {
        $this->load->helper('url');//辅助函数
        $id = $this->input->get('id');
        $this->load->model('TestModel', 'test');
        if ($id) {
            $this->test->del(array('id' => $id));
            echo '<script>alert("删除成功");window.location.href="'.site_url('Home/home/index').'"</script>';
        }
    }
}